﻿using CommonLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YouErYuanBLL;
using YouErYuanModels;

namespace YouErYuanManage.Controllers
{
    public class ChaKanSettingController : Controller
    {
        [YouErYuanManage.Filter.IsLogin]
        public string InsertClass()
        {
            TClass tc = new TClass();
            TClassBLL tcbll = new TClassBLL();
            tc.Classname = Request["className"];
            tc.Qvdate = Request["qvDate"];
            tc.Schoolid = (Session["user"] as Teacher).Schoolid;
            int count = tcbll.InsertClass(tc);
            return CommonMethod.FormatJsonObj(count, "");
        }

        //分页，获取幼儿园列表
        [YouErYuanManage.Filter.IsLogin]
        public string ChaKanSettingPageList()
        {
            ChaKanSettingBLL ckbll = new ChaKanSettingBLL();
            int pageIndex = 0;
            int pageSize = 0;
            try
            {
                pageIndex = Convert.ToInt32(Request["pageIndex"]);
                pageSize = Convert.ToInt32(Request["pageSize"]);
            }
            catch (Exception)
            {
                pageIndex = 1;
                pageSize = 10;
            }
            string schoolid = (HttpContext.Session["user"] as Teacher).Schoolid;
            int total = 0;
            object dt = ckbll.ChaKanSettingPageList(pageIndex, pageSize, schoolid, out total);
            int pageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;//总页数
            var data = new
            {
                pageIndex = pageIndex,
                pageCount = pageCount,
                list = dt,
                total = total
            };
            return CommonMethod.FormatJsonObj("", data);
        }

        //班级成员列表
        public string ChaKanClassList(string classId)
        {
            ChaKanSettingBLL ckbll = new ChaKanSettingBLL();
            object dt = ckbll.ChaKanClassList(classId);
            var data = new
            {
                list = dt
            };
            return CommonMethod.FormatJsonObj("", data);
        }

        /// <summary>
        /// 验证家长手机号是否存在
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public string CheckPhone(string phone)
        {
            int count = new JiaZhangBLL().CheckPhone(phone);
            if (count >= 1)
            {
                return CommonMethod.FormatJsonObj(1);
            }
            else
            {
                return CommonMethod.FormatJsonObj(0);
            }
        }

      

        /// <summary>
        /// 修改家长手机号、密码
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="phone"></param>
        /// <param name="jzId">家长id</param>
        /// <returns></returns>
        public string UpdateJiaZhang(string pwd, string phone, string jzId,string oldPhone)
        {
            if (string.IsNullOrEmpty(phone)||string.IsNullOrWhiteSpace(phone))
            {
                return CommonMethod.FormatJsonObj(2);
            }
            if (string.IsNullOrEmpty(pwd) || string.IsNullOrWhiteSpace(pwd))
            {
                return CommonMethod.FormatJsonObj(3);
            }
            if (string.IsNullOrEmpty(jzId) || string.IsNullOrWhiteSpace(jzId))
            {
                return CommonMethod.FormatJsonObj(4);
            }
            if (!oldPhone.Equals(phone))
            {
                //验证唯一
                int count = new JiaZhangBLL().CheckPhone(phone);
                if (count>=1)
                {
                    return CommonMethod.FormatJsonObj(5);
                }
            }
            //密码加密
            string pwd_jiami = DESencodingTxt.DESencrypt(pwd);
            int efcount = new JiaZhangBLL().UpdateJiaZ(jzId, phone, pwd_jiami);
            if (efcount>0)
            {
                return CommonMethod.FormatJsonObj(1);
            }
            else
            {
                return CommonMethod.FormatJsonObj(0);
            }           
        }


    }
}